<?php

namespace Modules\Car\Http\Controllers\Api;

use App\Events\NotifyEvent;
use App\Helpers\Constant;
use App\Helpers\PayTrait;
use App\Helpers\ResponseData;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\RecognizeDrivingLicense;
use Modules\Car\Http\Requests\Order\OrderRequest;

//店长确认订单
class StoreManagerConfirmController extends Controller
{
    use ResponseData,PayTrait;
    public function list(Request $request){
        $user = $request->user();
        if(!$user->department){
            $this->failed('请使用店长登录3',201);
        }

        $model = CarOrder::query()
            ->with('user','customer')
            ->where('status',Constant::ORDER_CONFIRM)
            ->where('department',$user->department_id);
        if($request->filled('search')){
            $model->where('phone',$request->search)
                ->orWhere('wx_name',$request->search)
                ->orWhere('nickname',$request->search)
                ->orWhere('nickname',$request->search)
                ->orWhere('customer_name',$request->search)
                ->orWhere('gear_car_name',$request->search)
                ->orWhere('plate_number',$request->search);
        }
        if($request->filled('is_online')){
            if($request->is_online==1){
                $model->where('openid','<>','');
            }else{
                $model->where('openid','');
            }

        }
        $data = $model->get();
        return $this->success($data);
    }


    public function update(OrderRequest $request){
        $user = $request->user();
        //店长派单
        if($request->filled('status')&&$request->status==Constant::ORDER_USING){
            $data = ['status'=>Constant::ORDER_USING];
            if($request->filled('mark')){
                $data['mark'] = $request->mark;
            }
            $order = CarOrder::query()
                ->where('order_sn',$request->order_sn)
                ->where('status',Constant::ORDER_IS_CONFIRM)
                ->where('department',$user->department_id)->first();
                $data['dispatch_manager'] = $user->id;
                $car = $this->car($order->gear_car_id);
                if(!$car){
                    return $this->failed('派单车辆不存在!',201);
                }
                $data['plate_number'] = $car->plate_number;
                $data['send_employer_id'] = $request->send_employer_id??0;
                $data['send_employer_nickname'] = $request->send_employer_nickname??'';
                $car->use_status = Constant::CAR_USE_STATUS_USING;
                $car->save();
                $order->update($data);
            event(new NotifyEvent($order,config('wechat.notify.mini.Dispatch')));
            return $this->success('店长派单成功!');
        }
        //店长取消订单
        if($request->filled('status')&&$request->status==Constant::ORDER__MANAGER_CANCLE){
            $order = CarOrder::query()
                ->where('order_sn',$request->order_sn)
                ->where('status',Constant::ORDER_CONFIRM)
                ->where('department',$user->department_id)
                ->first();
            if($order) {
                if ($order->out_trade_no) {
                    $total = $order->mini_pay * 100;
                    $this->miniRefund($order->order_sn, $order->out_trade_no, $total, $total);
                }
                $order->status = Constant::ORDER__MANAGER_CANCLE;
                $order->save();
                return $this->success('店长取消订单成功!');
            }
        }
    }
    protected function car($id){
        return RecognizeDrivingLicense::query()->find($id);
    }
    //返回未被使用的车辆
    public function UnusedCar(RecognizeDrivingLicense $model,Request $request){
        $size = $request->input('size',10);
        if($request->filled('search')){
            $search = $request->search;
            $model = $model
                ->with('carSeries')
                ->where('temp_finish',1)
                ->where('is_used',0)
                ->where(function($query)use($search){
                    $query
                        ->orWhere('gear_car_name','like','%'.$search.'%')
                        ->orWhere('plate_number','like','%'.$search.'%');
                })
            ->paginate($size);
            return $this->success($model);
        }
        if($request->filled('category')){
            $model = $model
                ->with('carSeries')
                ->where('category',$request->category)
                ->where('is_used',0)
                ->orderByDesc('id')
                ->paginate($size);
        }else{
            $model = RecognizeDrivingLicense::query()
                ->with('carSeries')
                ->where('is_used',0)
                ->orderByDesc('id')
                ->get();
        }
        return $this->success($model);
    }
}
